package com.example.boxugu.myapplication.Fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.ColorSpace;
import android.net.Uri;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.boxugu.myapplication.Moder.PracticeModer;
import com.example.boxugu.myapplication.R;
import com.example.boxugu.myapplication.activity.Practisedetai_lActivity;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PracticeFragment extends Fragment {
    ListView listView;
    MyAdapter myAdapter;
    ArrayList<PracticeModer.Data> list=new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_practice, container, false);
        listView=view.findViewById(R.id.practive_listview);
        myAdapter=new MyAdapter();
        RequestParams requestParams=new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/exerciselist");
        x.http().post(requestParams, new Callback.CacheCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson=new Gson();
                PracticeModer practiceModer =gson.fromJson(result, PracticeModer.class);
                if(practiceModer!=null&&practiceModer.data!=null){
                    list=practiceModer.data;
                    listView.setAdapter(myAdapter);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
        return view;
    }
    public class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final PracticeModer.Data data=list.get(position);
            ViewHolder viewHolder;
            if (convertView==null){
                viewHolder=new ViewHolder();
                convertView=View.inflate(getActivity(),R.layout.practice_listview,null);
                viewHolder.mposition=convertView.findViewById(R.id.positiontext);
                viewHolder.mtitle=convertView.findViewById(R.id.title);
                viewHolder.mcount=convertView.findViewById(R.id.textpassge);
                viewHolder.xian=convertView.findViewById(R.id.xian);
                viewHolder.c1Layout=convertView.findViewById(R.id.c1);
                convertView.setTag(viewHolder);
            }else{
                viewHolder=(ViewHolder)convertView.getTag();
            }
            viewHolder.mposition.setText(position+1+"");
            viewHolder.c1Layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(getActivity(), Practisedetai_lActivity.class);
                    intent.putExtra("id",data.id);
                    startActivity(intent);
                }
            });
            viewHolder.mtitle.setText(list.get(position).title);
            viewHolder.mcount.setText("共五题");
            viewHolder.xian.setText("");
            return convertView;
        }

        public class ViewHolder {
            TextView mposition;
            TextView mtitle;
            TextView mcount;
            TextView xian;
            ConstraintLayout c1Layout;

        }
    }

}
